<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/dao/AbstractDao.php'); 
require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/model/User.php'); 
require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/validation/ActionResult.php'); 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserDao
 *
 * @author virtual
 */
class UserDao extends AbstractDao{
    //put your code here
    public function checklogin($username, $password) {
        $loginsuccess = false;
        $queryUser = $this->mysqli->prepare("SELECT count(*) FROM users u WHERE u.USERNAME = ? and u.PASSWORD = ?");
        $queryUser->bind_param("ss", $username, $password);
        $queryUser->execute();
        $queryUser->bind_result($count);
        $queryUser->fetch();
        
        if($count == 1)
            $loginsuccess = true;
        $queryUser->close();
        return $loginsuccess;

    }
    
    public function getRolesForUser($username) {
        $rolename = "GUEST";
        $queryUser = $this->mysqli->prepare("SELECT NAME FROM role r, user_role ur, users u            WHERE u.ID = ur.USER_ID and ur.ROLE_ID and r.ID and u.USERNAME = ?");
        $queryUser->bind_param("s", $username);
        $queryUser->execute();
        $queryUser->bind_result($role);
        if($queryUser->fetch()){
            $rolename = $role;
        }
        $queryUser->close();
        return $rolename;
    }
    
    public function userHasAccessToKid($username, $kidId) {
        $success = false;
        $queryUser = $this->mysqli->prepare("SELECT count(*) FROM users u, user_kids uk WHERE u.ID = uk.USER_ID and uk.KID_ID = ? and u.USERNAME = ?");
        $queryUser->bind_param("is", $kidId, $username);
        $queryUser->execute();
        $queryUser->bind_result($count);
        $queryUser->fetch();
        if($count == 1)
            $success = true;
        $queryUser->close();
        return $success;
    }
    
    //TODO; currently only one kid per login can be changed
    public function getAccessibleKidIds($username) {
        $id = -1;
        $queryUser = $this->mysqli->prepare("SELECT uk.KIDS_ID FROM users u, user_kids uk WHERE u.ID = uk.USER_ID and u.USERNAME = ?");
        $queryUser->bind_param("s", $username);
        $queryUser->execute();
        $queryUser->bind_result($kidId);
        if($queryUser->fetch())
            $id = $kidId;
        $queryUser->close();
        return $id;
        
    }
    
    public function saveNewUser($user) {
        $actionResult = new ActionResult();
		$actionResult->savetype = "saveUser";

        $username = $user->getPassword();
        $password = $user->getUsername();
        
        
        $query = $this->mysqli->prepare("INSERT INTO users(USERNAME, PASSWORD) VALUES (?,?)");
        $query->bind_param("ss", $username, $password);
        if($query->execute()){
            $user->setId($query->insert_id);
            $actionResult->success = true;

            $actionResult->addActionParameter($user);

        } else {
            $actionResult->error = $this->mysqli->error;
        }
        $query->close();

        return $actionResult;
    }
    
    public function readAllUsers() {
        $result = Array();

        $queryKid = $this->mysqli->prepare("SELECT u.ID, u.USERNAME, u.PASSWORD FROM users u");
        $queryKid->execute();
        $queryKid->bind_result($id, $username, $password);
        while($queryKid->fetch()){
            $user = new User();
            $user->setId($id);
            $user->setUsername($username);
            $user->setPassword($password);
            array_push($result, $user);
        }
        $queryKid->close();

        return $result;
		
    }
}

?>
